<?php namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Services\Wechat\WechatLogin;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Log;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Support\Collection;
use EasyWeChat\Payment\Order;

class WechatController extends Controller
{
    public function index(){
        Log::info('request arrived.');
        $app    = app("wechat");
        $server = $app->server;
        $user = $app->user;
        $menu = $app->menu;
        $server->setMessageHandler(function($message) use ($user) {
            $fromUser = $user->get($message->FromUserName);
            $openid   = $message->FromUserName;
            switch($message->MsgType){
                case 'event':
                    if($message->Event == "subscribe"){
                        return "您好，欢迎关注";
                    }
                    break;
                case 'text':
                    return "感谢您的关注！";
                    break;
            }
        });
        Log::info('return response.');
        $buttons =[
                [
                    "name" => "团务系统",
                    "sub_button" => [
                        [
                            "type" => "view",
                            "name" => "团务知识",
                            "url"  => route("Home.Page.menu"),
                        ],
                        [
                            "type" => "view",
                            "name" => "青年之声",
                            "url"  => route("Home.Page.menu"),
                        ],
                        [
                            "type" => "view",
                            "name" => "智慧团建",
                            "url"  => route("Home.Page.menu"),
                        ],
                        [
                            "type" => "view",
                            "name" => "志愿者系统入口",
                            "url"  => route("Home.Page.menu"),
                        ],
                    ]
                ],
        ];
        $menu->add($buttons);
        $a = $server->serve();

        return $a;
    }

    public function getJsApiPacket(Request $request){
        $info = WechatLogin::getTicketByAccessToken();
        $jsapiTicket = $info->getTicket();

        $nonceStr = WechatLogin::createNonceStr();

        $timestamp = time();

        $url = urldecode($request->get('url'));

        Log::error('link===>'.$url);

        $config = new config();
        $appid  = $config->getAppid();

        $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
        $signature = sha1($string);
        $signPackage = array(
            "appId"     => $appid,
            "nonceStr"  => $nonceStr,
            "timestamp" => $timestamp,
            "url"       => $url,
            "signature" => $signature,
            "rawString" => $string
        );
        return response()->json(['level'=>'success','data'=>$signPackage]);
    }
}